
import { SignInRecord } from './../domain/sign-in-record';
import { Injectable } from '@angular/core';
import { ApiService } from './api.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
/*

@Author:赵明洋

@Date: 2018-12-12

@员工打卡服务

@Last Modified time: 2018-12-16
*/
const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
export class SignInTest {
  user_id: number;
  day:string;
  sign_in: string; //上班打卡
  sign_out: string; //下班打卡

}



@Injectable({
  providedIn: 'root'
})
export class SignInService {

  private api_url;
  private headers;

  constructor(private apiService: ApiService,
    private http: HttpClient) 
    {
      this.api_url = apiService.getUrl();
    this.headers = apiService.getHeaders();
     }
        
     //获取打卡记录信息
     getSignInRecords (id:number): Observable<SignInRecord[]> {
      return this.http.get<SignInRecord[]>(this.api_url + "/sign_in_records?user_id="+id)
      .pipe(
        catchError(this.handleError('getSignInRecords', []))
      );
    }

    getSignInRecord(date:string,id:number): Observable<SignInRecord[]>
    {
        return this.http.get<SignInRecord[]>(this.api_url + "/sign_in_records"+"?user_id="+id + "&day="+date);
        
    }  



     private handleError<T>(operation = 'operation', result?: T) {
      return (error: any): Observable<T> => {
  
        // TODO: send the error to remote logging infrastructure
        console.error(error); // log to console instead
  
        // TODO: better job of transforming error for user consumption
        console.log(`${operation} failed: ${error.message}`);
  
        // Let the app keep running by returning an empty result.
        return of(result as T);
      };
    }


    addSignInRecord (signInTest: SignInTest): Observable<any> {
      return this.http.post(this.api_url + "/sign_in_records", signInTest, this.headers)
        .pipe(
          catchError(this.handleError('addSignInRecord', signInTest))
        );
    }

  
    
    upDateSignInRecord (signInTest: SignInTest,id:number): Observable<any> {
      return this.http.put(this.api_url + "/sign_in_records/"+id, signInTest, this.headers)
        .pipe(
          catchError(this.handleError('upDateSignInRecord', signInTest))
        );
    }




}
